#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
int main()
{
	pid_t id = fork();
	if (id == 0)
	{
		//int cnt = 5;
		while(1)
		{
			printf("我是子进程，pid: %d, ppid: %d\n", getpid(), getppid());
			sleep(1);
			//cnt--;
		}
		exit(40);
	}
	int status = 0;
	pid_t rid = waitpid(id, &status, 0);
	if (rid > 0)
	{
		printf("wait success, rid: %d, exit code: %d, exit signal: %d\n", rid, (status>>8)&0xFF, status&0x7F);	//rid为成功等待的子进程的pid
	}
	else
	{
		printf("wait failed: %d: %s\n", errno, strerror(errno));
	}
	return 0;
}
//void fun()
//{
//	printf("fun begin!\n");
//	exit(40);
//	printf("fun end!\n");
//}
//
//int main()
//{
//	printf("hello world!");
//	_exit(40);
////	fun();
////	printf("main!\n");
////	for (int i = 0; i < 150; i++)
////	{
////		printf("%d->%s\n", i, strerror(i));
////	}
//
//	//FILE* fp = fopen("log.txt", "r");
//	//if (fp == NULL)
//	//	return 1;
//	return 0;
//	
//}
